package com.shiguiwu.springmybatis.spring.aop.principle;

import org.springframework.aop.ThrowsAdvice;
import org.springframework.aop.framework.ProxyFactory;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @description: 异常通知
 * @author: stone
 * @date: Created by 2021/5/20 20:32
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.aop.principle
 */
public class ThrowAdviceTests {

    public static class SendMsgThrowsAdvice implements ThrowsAdvice {


        //方法名，必须为afterThrowing
        public void afterThrowing(Method method, Object[] objects, Object target, RuntimeException e) {
            System.out.println("异常报警：");
            System.out.println(String.format("method:[%s], args:[%s]", method.toGenericString(),
                    Arrays.stream(objects).collect(Collectors.toList())));
            System.out.println(e.getMessage());
            System.out.println("请尽快修复bug!!!");

        }
    }

    public static void main(String[] args) {
        //代理工厂
        ProxyFactory factory = new ProxyFactory(new FunsService());

        //添加异常通知
        factory.addAdvice(new SendMsgThrowsAdvice());

        Object proxy = factory.getProxy();
        FunsService funsService = (FunsService) proxy;

        funsService.cashOut("路人", 100);
        funsService.cashOut("路人甲", 2000);
    }
}
